<?php

namespace app\api\controller\v38;

use app\api\controller\v2\Auth;
use app\common\cachemodel\Recode as RecodeModel;
use app\common\cachemodel\zl4\Zl4HoroscopeReport;
use app\common\lib\Util;
use think\Env;
use think\response\Json;

class Horoscope extends Auth
{
    /**
     * 72格局判定 详情
     * @return Json
     * @throws \Exception
     */
    public function Pattern72Decision()
    {
        $rid = input('rid/d', 0);

        $recodeInfo = (new RecodeModel())->getInfoById($rid);
        if (!$recodeInfo || $recodeInfo['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '请求异常，档案不存在～');
        }

        $HoroscopeReportModel = new Zl4HoroscopeReport();

        $HoroscopeReportInfo = $HoroscopeReportModel->getReport($recodeInfo, [1]);

        if (empty($HoroscopeReportInfo))
        {
            return show(config('code.error'), '服务异常，请稍后再试～');
        }
        $HoroscopeReportInfo = current($HoroscopeReportInfo);

        if ($HoroscopeReportInfo['status'] !== 2)
        {
            return show(config('code.error'), '服务异常，请稍后再试～');
        }

        $content = $HoroscopeReportInfo['content'] ?? [];
        if (empty($content))
        {   // 重新计算
            Util::queuePush('horoscope_report_calculate', ['id' => $HoroscopeReportInfo['id']], 0);

            return show(config('code.error'), '服务异常，请稍后再试～');
        }

        $oss = Env::get('host.photo');
        $neige = [
            'title_img' => $oss . '/test/horoscope/title2_ngjpdjx.png',
            'des' => '',
            'item' => [],
        ];

        $totalScore = 0;
        $totalScore2 = 0;
        if (isset($content['neige']) && $content['neige'])
        {
            $content['neige']['score1'] = ceil($content['neige']['score1']);
            $content['neige']['score1'] = min($content['neige']['score1'], $content['neige']['score1']);
            $totalScore += $content['neige']['score1'];
            $totalScore2 += $content['neige']['score2'];
            $neige['item'][] = [
                'icon' => $oss . '/test/horoscope/icon1_1.png',
                'title' => $content['neige']['title'],
                'content' => $content['neige']['content'],
                'tag_info' => [
                    'text_color' => '#EE904B',
                    'bg_color' => '#FFF9F3',
                    'bg_border_color' => '#F9CEA4',
                    'tag_list' => $content['neige']['tag_list'],
                    'title' => '格局影响程度：' . number_format($content['neige']['score1'] / $content['neige']['score2'], 2) * 100 . '%',
                    'icon' => $oss . '/test/horoscope/icon1_2.png',
                    'content' => '',
                ],
                'type' => 1,
            ];
        } else
        {
            $neige['item'][] = [
                'icon' => '',
                'title' => '',
                'content' => '该档案暂无内格局',
                'tag_info' => null,
                'type' => 2,
            ];
        }
        $waige = [
            'title_img' => $oss . '/test/horoscope/title2_wgjpdjx.png',
            'des' => '外格不是必然存在的，大多数也仅是起到锦上添花的作用，因此赋分较低。请以参考内格为主。',
            'item' => []
        ];
        if (isset($content['waige']) && $content['waige'])
        {
            foreach ($content['waige'] as $value)
            {
                $value['score1'] = ceil($value['score1']);
                $value['score1'] = min($value['score2'], $value['score1']);
                $totalScore += $value['score1'];
                $totalScore2 += $value['score2'];
                $waige['item'][] = [
                    'icon' => $oss . '/test/horoscope/icon2_1.png',
                    'title' => $value['title'],
                    'content' => $value['content'],
                    'tag_info' => [
                        'text_color' => '#9266F2',
                        'bg_color' => '#F9F6FF',
                        'bg_border_color' => '#DCD1F1',
                        'tag_list' => $value['tag_list'],
                        'title' => '格局影响程度：' . number_format($value['score1'] / $value['score2'], 2) * 100 . '%',
                        'icon' => $oss . '/test/horoscope/icon2_2.png',
                        'content' => '',
                    ],
                    'type' => 1,
                ];
            }
        } else
        {
            $waige['item'][] = [
                'icon' => '',
                'title' => '',
                'content' => '该档案暂无外格局',
                'tag_info' => null,
                'type' => 2,
            ];
        }

        $list = [
            [
                'title_img' => $oss . '/test/horoscope/title_js.png',
                'des' => '',
                'item' => [
                    [
                        'icon' => '',
                        'title' => '',
                        'content' => '格局是指通过个人八字排盘后呈现出来的整体状态会对你的人生格局进行综合性的总结。本内容会针对你的个人档案信息的多个维度对你的人生格局进行总结。',
                        'tag_info' => null,
                        'type' => 2,
                    ],
                ],
            ],
            $neige,
            $waige,
            [
                'title_img' => $oss . '/test/horoscope/title2_ztgj.png',
                'des' => '',
                'item' => [
                    [
                        'icon' => '',
                        'title' => '',
                        'content' => '',
                        'tag_info' => [
                            'text_color' => '#E07C25',
                            'bg_color' => '#FFF5F2',
                            'bg_border_color' => '#EECDC3',
                            'tag_list' => $content['zhengti']['tag_list'],
//                            'title' => '格局影响程度：' . number_format($totalScore / $totalScore2, 2) * 100 . '%',
                            'title' => '格局影响程度：' . ($totalScore2 ? (number_format($totalScore / $totalScore2, 2) * 100) : '5') . '%',
                            'icon' => $oss . '/test/horoscope/icon3_2.png',
                            'content' => $content['zhengti']['content'],
                        ],
                        'type' => 1,
                    ],
                ],
            ]
        ];

        $recodeData = (new Util())->getFortuneDetailRecodeInfo([$HoroscopeReportInfo['recode_info']], $this->user['rid']);
        $unlockRecodeInfo = [
            'recode_is_change' => 0,
            'info' => $recodeData,
            'change_title' => '',
            'change_desc' => '',
            'target' => '',
            'link' => '',
            'btn_name' => '',
        ];
        if ($HoroscopeReportInfo['recode_is_change'] === true)
        {   // 档案已更新 或者 状态在计算中 展示档案更新提示
            $unlockRecodeInfo['recode_is_change'] = 1;
            $unlockRecodeInfo['change_title'] = '检测到当前档案出生时间已发生修改';
            $unlockRecodeInfo['change_desc'] = '已计算的内容不会随着档案的修改重新计算。';
            $unlockRecodeInfo['target'] = 'local';
            $unlockRecodeInfo['link'] = "pay_order?type=28&rid={$rid}&gid=107&need_auth=1";
            $unlockRecodeInfo['btn_name'] = '再次开通';
        }

        $resData = [
            'list' => $list,
            'unlock_recode_info' => $unlockRecodeInfo,
        ];

        return show(config('code.success'), '请求成功', $resData);
    }
}
